package com.come4loves.game.module.fight.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class AttackListModel {
	private Collection<FightCard> attackerList;
	private List<FightCard> mainCards;
	
	private List<FightCard> indexCards = new ArrayList<FightCard>();
	
	private Map<FightCard, List<FightCard>> chooseTarget;
	
	private int index;
	
	private ActorBattle actorBattle;
	public AttackListModel(ActorBattle actorBattle) {
		this.actorBattle = actorBattle;
		this.attackerList = actorBattle.getShowCards().values();
		this.mainCards = actorBattle.getMainCards();
		this.chooseTarget = actorBattle.getCurrentTarget();
		
		this.indexCards.addAll(mainCards);
		this.indexCards.addAll(attackerList);
		Collections.sort(this.indexCards, new SpeedComparator());
	}
	public FightCard hand(){
		while (true) {
			FightCard result = indexCards.get(index);
			index += 1;
			if (!result.isDead()&&result.isAction()) {
				return result;
			}
			
			if (index >= indexCards.size()) {
				break;
			}
			
		}
		return null;
	}
	
	public boolean handComplete(){
		return index >= indexCards.size();
	}
	/**
	 * 主将死亡视为全部死亡
	 * @return
	 */
	public boolean isAllDead(){
		for (FightCard fightCard : mainCards) {
			if (!fightCard.isDead()){
				return false;
			}
		}
		return true;
	}
	
	
	
	public void reset(){
		index = 0;
	}
	
	public Collection<FightCard> getAttackerList() {
		return attackerList;
	}
	public List<FightCard> getMainCards() {
		return mainCards;
	}
	
	public List<FightCard> getChooseTarget(FightCard fightCard) {
		if (chooseTarget == null || chooseTarget.isEmpty()) {
			return null;
		}
		return this.chooseTarget.get(fightCard);
	}
	
	public ActorBattle getActorBattle() {
		return actorBattle;
	}
}
